package com.moxiao.yfjh.module.blind.service.distributionsharing;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.moxiao.yfjh.framework.common.pojo.PageResult;
import com.moxiao.yfjh.module.blind.controller.admin.distributionsharing.vo.DistributionSharingCreateReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.distributionsharing.vo.DistributionSharingExportReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.distributionsharing.vo.DistributionSharingPageReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.distributionsharing.vo.DistributionSharingUpdateReqVO;
import com.moxiao.yfjh.module.blind.convert.distributionsharing.DistributionSharingConvert;
import com.moxiao.yfjh.module.blind.dal.dataobject.distributionsharing.DistributionSharingDO;
import com.moxiao.yfjh.module.blind.dal.mysql.distributionsharing.DistributionSharingMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.Objects;

import static com.moxiao.yfjh.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.moxiao.yfjh.module.blind.enums.ErrorCodeConstants.DISTRIBUTION_SHARING_NOT_EXISTS;

/**
 * 相亲分销分享 Service 实现类
 *
 * @author mx
 */
@Service
@Validated
public class DistributionSharingServiceImpl implements DistributionSharingService {

    @Resource
    private DistributionSharingMapper distributionSharingMapper;

    @Override
    public Long createDistributionSharing(DistributionSharingCreateReqVO createReqVO) {
        // 插入
        DistributionSharingDO distributionSharing = DistributionSharingConvert.INSTANCE.convert(createReqVO);
        distributionSharingMapper.insert(distributionSharing);
        // 返回
        return distributionSharing.getId();
    }

    @Override
    public void updateDistributionSharing(DistributionSharingUpdateReqVO updateReqVO) {
        // 校验存在
        validateDistributionSharingExists(updateReqVO.getId());
        // 更新
        DistributionSharingDO updateObj = DistributionSharingConvert.INSTANCE.convert(updateReqVO);
        distributionSharingMapper.updateById(updateObj);
    }

    @Override
    public void deleteDistributionSharing(Long id) {
        // 校验存在
        validateDistributionSharingExists(id);
        // 删除
        distributionSharingMapper.deleteById(id);
    }

    private void validateDistributionSharingExists(Long id) {
        if (distributionSharingMapper.selectById(id) == null) {
            throw exception(DISTRIBUTION_SHARING_NOT_EXISTS);
        }
    }

    @Override
    public DistributionSharingDO getDistributionSharing(Long id) {
        return distributionSharingMapper.selectById(id);
    }

    @Override
    public List<DistributionSharingDO> getDistributionSharingList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return distributionSharingMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<DistributionSharingDO> getDistributionSharingPage(DistributionSharingPageReqVO pageReqVO) {
        return distributionSharingMapper.selectPage(pageReqVO);
    }

    @Override
    public List<DistributionSharingDO> getDistributionSharingList(DistributionSharingExportReqVO exportReqVO) {
        return distributionSharingMapper.selectList(exportReqVO);
    }

    @Override
    public Long getUserId(DistributionSharingPageReqVO pageReqVO) {
        LambdaQueryWrapper<DistributionSharingDO> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(pageReqVO.getIp()), DistributionSharingDO::getIp, pageReqVO.getIp());
        lqw.eq(StringUtils.isNotBlank(pageReqVO.getDeviceType()), DistributionSharingDO::getDeviceType, pageReqVO.getDeviceType());
//        lqw.eq(StringUtils.isNotBlank(pageReqVO.getDeviceId()),DistributionSharingDO::getDeviceId,pageReqVO.getDeviceId());
        lqw.eq(StringUtils.isNotBlank(pageReqVO.getSystem()), DistributionSharingDO::getSystem, pageReqVO.getSystem());
        lqw.orderByDesc(DistributionSharingDO::getCreateTime);
        lqw.last("LIMIT " + 1);
        DistributionSharingDO distributionSharingDO = distributionSharingMapper.selectOne(lqw);
        if (Objects.nonNull(distributionSharingDO)) {
            return distributionSharingDO.getShareUserId();
        } else {
            return -1L;
        }
    }

}
